******************************************************************************************
* Do-file name:	an_reg_wage_indv_01.do                                                   
* Task:         estimate pure wage effect (incl. age in baseline), full and sub-samples       
* Last change:  28.07.2025                                                               
* Notes:                                                                                 
/*
This file contains the code used to generate the results presented in the following tables and figures:
- Figure 1, Panel B: 
		-- Pure wage effect: pe_s1

- Table 2:
		-- Pure wage effect: pe_s1

- Table 3:
		-- Pure wage effect, Panel B: pe_s6		
		-- Pure wage effect, Panel C: Routine: pe_s9; Abstract: pe_s10

- Table A.4:
		-- Pure wage effect: pe_s1
*/
******************************************************************************************



******************************************************************************************
*** program setup
******************************************************************************************

version 14.2
clear all
macro drop _all
set linesize 90
set more off
* set trace on
discard
set seed 123456789
*set matsize 2000



******************************************************************************************
*** load working dataset
******************************************************************************************

use "data/work_indiv.dta", clear


*** restrict sample
keep if y_nat != .
keep if ao_gem_imp != .

*** drop variables
drop d_emp_* d_wage_* y_to* 

*** create outcome: price effect
sort vsnr_ano year
tsset vsnr_ano year

** bring wage to row of year 1990
foreach x of numlist 1/5 {
local y = 1990 - `x'
gen y_nat_`y' = L`x'.y_nat  if year == 1990
	}

foreach x of numlist 1/5 {
local y = 1990 + `x'
gen y_nat_`y' = F`x'.y_nat  if year == 1990
	}

gen y_nat_1990 = y_nat  if year == 1990


** native wage growth: here as price effect (wage growth of stayers in the same region)
foreach y of numlist 1985/1995 {
gen d_wage_nat_`y'_90 = y_nat_`y' - y_nat_1990  if year == 1990 & nation_gr == 1 & ao_gem_imp_1990 == ao_gem_imp_`y' & ao_gem_imp_1990 != . & ao_gem_imp_`y' != .
	}

*** restrict sample
keep if year == 1990



******************************************************************************************
*** set globals
******************************************************************************************

*** baseline controls
global controls_base  "age"

*** baseline restrictions for wage regerssions
global base_restric_wa "(border_imp_13 == 1 | control_imp == 1) & year == 1990 & nation_gr == 1 & age >= 16 & age <= 65 & weight_fte != ."

* 1) 
global restric_wage_1 "$base_restric_wa"
global sample_wage_1  "All full-time employed workers (natives)"

// sub-sample: age-groups
* 6) 
global restric_wage_6 "$base_restric_wa & age_3 == 3"
global sample_wage_6  "Age 50 and above (natives)"

// sub-sample: occupation tasks
* 9) 
global restric_wage_9 "$base_restric_wa & task_3dig_2 == 1"
global sample_wage_9  "Manual/routine tasks worker (3d) (natives)"

* 10) 
global restric_wage_10 "$base_restric_wa & task_3dig_2 == 2"
global sample_wage_10  "Abstract tasks worker (3d) (natives)"


*** use different czech shocks
foreach year of numlist 1987/1990 1992/1995 {
global czech_shock_`year' "czshare_92_90_predic_13"
	}

global czech_shock_1991   "czshare_91_90_predic_13"



******************************************************************************************
*** estimate price effect
******************************************************************************************

*** run different samples 
foreach y of numlist 1 6 9 10 {

foreach x of numlist 1987/1995 {

*** 1) basic model
reg d_wage_nat_`x'_90  ${czech_shock_`x'} $controls_base  if ${restric_wage_`y'} [pweight=weight_matching], cluster(ao_kreis_imp)
est store pe_`x'_90_s`y'


*** check sample
dis "Sample: ${sample_wage_`y'}"
dis "Sample: ${restric_wage_`y'}"
dis "Sample restriction version: 16-65, full-time employed native stayers"
count if e(sample)


********* bootstrap standard errors ***************************************************************
preserve
keep if e(sample)
drop czshare_92_90_predic_13
forval bs = 1/500 {
	di "bootstrap sample `bs'"
	merge m:1 ao_gem_imp using "data/bootstrap/bs_wild_first_92_`bs'.dta", nogenerate
	reg d_wage_nat_`x'_90  czshare_92_90_predic_13 $controls_base  if ${restric_wage_`y'} [pweight=weight_matching]
	predict res  if e(sample), res
	predict xb   if e(sample), xb
	gen y = xb + wild * res
	qui reg y  czshare_92_90_predic_13  $controls_base  if ${restric_wage_`y'} [pweight=weight_matching]
	scalar coef`bs' = _b[czshare_92_90_predic_13]
	drop y res xb czshare_92_90_predic_13 wild
}
gen bs = . 
gen coef_pe_s`y'_`x' = .
forval bs = 1/500 {
	replace bs = `bs' in `bs'
	replace coef_pe_s`y'_`x' = coef`bs' in `bs'
}
keep bs coef_pe_s`y'_`x'
keep if bs != .
save "data/bootstrap/price_effect\bs_coef_pe_s`y'_`x'.dta", replace
sum coef_pe_s`y'_`x'		// std. dev. = std. error
restore

***************************************************************************************************

********* bootstrap standard errors 1991 **********************************************************
if `y' == 1 & `x' == 1991 {
preserve
keep if e(sample)
drop czshare_91_90_predic_13
forval bs = 1/500 {
	di "bootstrap sample `bs'"
	merge m:1 ao_gem_imp using "data/bootstrap/bs_wild_first_91_`bs'.dta", nogenerate
	reg d_wage_nat_`x'_90  czshare_91_90_predic_13 $controls_base  if ${restric_wage_`y'} [pweight=weight_matching]
	predict res  if e(sample), res
	predict xb   if e(sample), xb
	gen y = xb + wild * res
	qui reg y  czshare_91_90_predic_13  $controls_base  if ${restric_wage_`y'} [pweight=weight_matching]
	scalar coef`bs' = _b[czshare_91_90_predic_13]
	drop y res xb czshare_91_90_predic_13 wild
}
gen bs = . 
gen coef_pe_s`y'_`x' = .
forval bs = 1/500 {
	replace bs = `bs' in `bs'
	replace coef_pe_s`y'_`x' = coef`bs' in `bs'
}
keep bs coef_pe_s`y'_`x'
keep if bs != .
save "data/bootstrap/price_effect\1991_cz_shock\bs_coef_pe_s`y'_`x'.dta", replace
sum coef_pe_s`y'_`x'		// std. dev. = std. error
restore
}
***************************************************************************************************
	}


*** create tables
#delimit ;
global estout_wage "cells(b(star fmt(%9.3f) vacant(-)) se(fmt(%9.3f) par)) 
stats(N N_clust r2_a, labels("Observations" "Clusters" "Adjusted R2") layout(@ @ @) fmt(%9.0fc %9.0fc %9.3f))
starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(30)
varlabels(_cons "Constant" czshare_92_90_predic_13 "Share Czechs (92-90, pred.)" age "Age") label
mlabels("Pr.Ef. 87-90" "Pr.Ef. 88-90" "Pr.Ef. 89-90" "Pr.Ef. 91-90" "Pr.Ef. 92-90" "Pr.Ef. 93-90" "Pr.Ef. 94-90" "Pr.Ef. 95-90")
prehead(@title) posthead() postfoot(@note) nonumbers collabels(none) style(tab)";
#delimit cr


** Tables: Price effect
estout 	pe_1987_90_s`y'  pe_1988_90_s`y'  pe_1989_90_s`y' pe_1991_90_s`y' ///
pe_1992_90_s`y'  pe_1993_90_s`y'  pe_1994_90_s`y'  pe_1995_90_s`y' ///
using "tables\price_effect/tab_pe_s`y'.txt", $estout_wage replace ///	
title(Native Wage Growth 1987-1995: Price Effect, Sample: ${sample_wage_`y'}) ///
note(Notes: Individual approach. Native stayers in region. * p<0.1, ** p<0.05, *** p<0.01. ///
Data Source: German Social Security Records, border districts and matched control districts, 1987-1995.)
	}


******************************************************************************************
*** end
******************************************************************************************

exit


*========================================================================================*
Comments:
- unique identifier: vsnr_ano year
